﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GBG_DBM.Support.DAL.Entities;
using GBG_DBM.Support.DAL;
using GBG_DBM.Support.DAL.DB;
using System.Text.RegularExpressions;
namespace GBG_DBM.UI
{
    public partial class STD_UI : Form
    {
		private List<string> Code = new List<string>(12);
		private float objective;
		private DBAccessory _DBAccessory = new DBAccessory();
		private float percentage = 0;
		private float package;
		private float mat = 0;
		private float water = 0;
		private float gross = 0;
		private STD _STD;
		private Formula _Formula;
        public STD_UI()
        {
            InitializeComponent();
        }
		public STD_UI(STD _STD) 
		{
			InitializeComponent();
		}
		public STD_UI(DataTable Supplier_RM,DataTable Package)
		{
			InitializeComponent();
			this.RetrieveData(Supplier_RM, Package);
			objective = 11.4F;
			txtObjective.Text = objective.ToString();
		}
		private void RetrieveData(DataTable Supplier_RM,DataTable Package) 
		{
			int packagecount = Package.Rows.Count;
			int Supplier_RMcount = Supplier_RM.Rows.Count;
			for (int t = 0; t < packagecount; t++) 
			{
				cbbJar.Items.Add(Convert.ToString(Package.Rows[t].ItemArray[0]));
			}
			cbbJar.Text = Convert.ToString(cbbJar.Items[0]);
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode1.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			cbbCode1.Text=Convert.ToString(cbbCode1.Items[0]);
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode2.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode3.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode4.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode5.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode6.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode7.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode8.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode9.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode10.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode11.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
			for (int a = 0; a < Supplier_RMcount; a++)
			{
				cbbCode12.Items.Add(Convert.ToString(Supplier_RM.Rows[a].ItemArray[0]));
			}
		}
		private void btnModifyObjective_Click(object sender, EventArgs e)
		{
			if (txtObjective == null)
			{
				MessageBox.Show("Please input appropriate object value");
			}
			else if (objective == 11.4)
			{
				MessageBox.Show("Please input specific object except 11.4%");
			}
			else
			{
				MessageBox.Show("The object has been successfully updated");
				objective = Convert.ToSingle(txtObjective.Text);
			}
		}
		private void btnCreate_Click(object sender, EventArgs e)
		{
            float percentage = 0;
			if(txtPercentage1.Text!=null){
				 percentage = Convert.ToSingle(txtPercentage1.Text);
			}
			if(txtPercentage2.Text!=null)
			{
                percentage+= Convert.ToSingle(txtPercentage2.Text);
			}
			if(txtPercentage3.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage3.Text);
			}
			if(txtPercentage4.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage4.Text);
			}
			if(txtPercentage5.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage5.Text);
			}
			if(txtPercentage6.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage6.Text);
			}
			if(txtPercentage7.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage7.Text);
			}
			if(txtPercentage8.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage8.Text);
			}
			if(txtPercentage9.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage9.Text);
			}
			if(txtPercentage10.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage10.Text);
			}
			if(txtPercentage11.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage11.Text);
			}
			if(txtPercentage12.Text!=null){
				percentage+= Convert.ToSingle(txtPercentage12.Text);}
			if(percentage==100)
            {


				BiogroCheck();
				_STD = new STD();
				_Formula = new Formula();
				_STD.ProductId = txtCode.Text;
				_STD.ProductName = rtbProductName.Text;
				_STD.ProductDescription = rtbProductDescription.Text;
				_STD.ProductIngredient = rtbProductIngredient.Text;
                _STD.Date = dtpTime.Value.ToString();
				_STD.RRP = Convert.ToSingle(txtRRP.Text);
				_STD.Description = txtDescription.Text;
				_STD.Comments = rtbComments.Text;
				_STD.RawMat = Convert.ToSingle(lblRawMat.Text);
				_STD.ProductPackage = Convert.ToSingle(lblPackage.Text);
				_STD.Total = Convert.ToSingle(lblTotal.Text);
                _STD.Biogro = 0.96;
				_STD.Objective = Convert.ToSingle(lblObject.Text);
				if (txtCostUnit1 != null)
				{
					Code.Add(cbbCode1.Text);
				}
				else if (txtCostUnit2 != null)
				{
					Code.Add(cbbCode2.Text);
				}
				else if (txtCostUnit3 != null)
				{
					Code.Add(cbbCode3.Text);
				}
				else if (txtCostUnit4 != null)
				{
					Code.Add(cbbCode4.Text);
				}
				else if (txtCostUnit5 != null)
				{
					Code.Add(cbbCode5.Text);
				}
				else if (txtCostUnit6 != null)
				{
					Code.Add(cbbCode6.Text);
				}
				else if (txtCostUnit7 != null)
				{
					Code.Add(cbbCode7.Text);
				}
				else if (txtCostUnit8 != null)
				{
					Code.Add(cbbCode8.Text);
				}
				else if (txtCostUnit9 != null)
				{
					Code.Add(cbbCode9.Text);
				}
				else if (txtCostUnit10 != null)
				{
					Code.Add(cbbCode10.Text);
				}
				else if (txtCostUnit11 != null)
				{
					Code.Add(cbbCode11.Text);
				}
				else if (txtCostUnit12 != null)
				{
					Code.Add(cbbCode12.Text);
				}
				//_Formula.Code = cbbCode1.SelectedItem.ToString();

				_Formula.ProductId = txtCode.Text;
				_DBAccessory.addSTD(_STD);
				_DBAccessory.addFormula(_Formula, Code);
			}
            else if (percentage < 100) 
			{
				MessageBox.Show("The percentage less than 100%");
                
			}
			else 
			{
				MessageBox.Show("The percentage more than 100%");
                
			}
			
			
		}
		private float BiogroCheck() 
		{
			if (txtPercentage1.Font.Bold == true) 
			{
				percentage = Convert.ToSingle(txtPercentage1.Text);
			}
			else if (txtPercentage2.Font.Bold == true) 
			{
				percentage += Convert.ToSingle(txtPercentage2.Text);
			}
			else if (txtPercentage3.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage3.Text);
			}
			else if (txtPercentage4.Font.Bold == true) 
			{
				percentage += Convert.ToSingle(txtPercentage4.Text);
			}
			else if (txtPercentage5.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage5.Text);
			}
			else if (txtPercentage6.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage6.Text);
			}
			else if (txtPercentage7.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage7.Text);
			}
			else if (txtPercentage8.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage8.Text);
			}
			else if (txtPercentage9.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage9.Text);
			}
			else if (txtPercentage10.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage10.Text);
			}
			else if (txtPercentage11.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage11.Text);
			}
			else if (txtPercentage12.Font.Bold == true)
			{
				percentage += Convert.ToSingle(txtPercentage12.Text);
			}
			percentage = percentage / (100 - water);
            //txtBiogro.Text = percentage.ToString();
			if (percentage < 0.75) 
			{
				MessageBox.Show("Disqualified,Please make change");
			}
			else if (percentage < 0.9)
			{
				MessageBox.Show("Elementary Level");
			}
			else 
			{
				MessageBox.Show("Ideal Level");
			}
            return percentage;

		}
		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.Close();
		}
		private void cbbJar_SelectedIndexChanged(object sender, EventArgs e)
		{
			string selected = cbbJar.Text;
			lblPackage.Text = Convert.ToString(_DBAccessory.getPackagePriceById(selected));
		}
		#region Code Combobox
		private void cbbCode1_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o=false;
			string selected=cbbCode1.Text;
			string[] SupplierName= _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++) 
			{
                if (SupplierName[a] != null)
                {
                    if (!cbbSupplName1.Items.Contains(SupplierName[a]))
                    {
                        cbbSupplName1.Items.Add(SupplierName[a]);
                    }
                }
                else 
                {
                    break;
                }
			}
            cbbSupplName1.Text = Convert.ToString(cbbSupplName1.Items[0]);
			cbbSupplName1.Enabled = true;
			if (COcheck(cbbCode1.SelectedItem.ToString(), o)) 
			{
				Font ft=new Font("",12,FontStyle.Bold);
				txtPercentage1.Font = ft;
			}
			if(txtPercentage1.Text!=null)
			{
				if (cbbCode1.Text.Contains("Water")) 
				{
					water += Convert.ToSingle(txtPercentage1.Text);
				}
			}	
		}
		private void cbbCode2_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode2.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {

                    if (!cbbSupplName2.Items.Contains(SupplierName[a]))
                    {
                        cbbSupplName2.Items.Add(SupplierName[a]);
                    }
                    
                }
                else 
                {
                    break;
                }
			}
            cbbSupplName2.Text = Convert.ToString(cbbSupplName2.Items[0]);
			cbbSupplName2.Enabled = true;
			if (COcheck(cbbCode2.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage2.Font = ft;
			}
			if (txtPercentage2.Text != null)
			{
				if (cbbCode2.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage2.Text);
				}
			}	
		}
		private void cbbCode3_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode3.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName3.Items.Add(SupplierName[a]);
                }
                else 
                {
                    break;
                }
			}
            cbbSupplName3.Text = Convert.ToString(cbbSupplName3.Items[0]);
;			cbbSupplName3.Enabled = true;
			if (COcheck(cbbCode3.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage3.Font = ft;
			}
			if (txtPercentage3.Text != null)
			{
				if (cbbCode3.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage3.Text);
				}
			}	
		}
		private void cbbCode4_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode4.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName4.Items.Add(SupplierName[a]);
                }
                else 
                {
                    break;
                }
			}
            cbbSupplName4.Text = Convert.ToString(cbbSupplName4.Items[0]);
			cbbSupplName4.Enabled = true;
			if (COcheck(cbbCode4.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage4.Font = ft;
			}
			if (txtPercentage4.Text != null)
			{
				if (cbbCode4.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage4.Text);
				}
			}	
		}
		private void cbbCode5_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode5.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName5.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
				
			}
            cbbSupplName5.Text = Convert.ToString(cbbSupplName5.Items[0]);
			cbbSupplName5.Enabled = true;
			if (COcheck(cbbCode5.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage5.Font = ft;
			}
			if (txtPercentage5.Text != null)
			{
				if (cbbCode5.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage5.Text);
				}
			}	
		}
		private void cbbCode6_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode6.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName6.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
				
			}
            cbbSupplName6.Text = Convert.ToString(cbbSupplName6.Items[0]);
			cbbSupplName6.Enabled = true;
			if (COcheck(cbbCode6.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage6.Font = ft;
			}
			if (txtPercentage6.Text != null)
			{
				if (cbbCode6.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage6.Text);
				}
			}	
		}
		private void cbbCode7_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode7.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName7.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
				
			}
            cbbSupplName7.Text = Convert.ToString(cbbSupplName7.Items[0]);
			cbbSupplName7.Enabled = true;
			if (COcheck(cbbCode7.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage7.Font = ft;
			}
			if (txtPercentage7.Text != null)
			{
				if (cbbCode7.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage1.Text);
				}
			}	
		}
		private void cbbCode8_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode8.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName8.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
				
			}
            cbbSupplName8.Text = Convert.ToString(cbbSupplName8.Items[0]);
			cbbSupplName8.Enabled = true;
			if (COcheck(cbbCode8.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage8.Font = ft;
			}
			if (txtPercentage8.Text != null)
			{
				if (cbbCode8.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage8.Text);
				}
			}	
		}
		private void cbbCode9_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode9.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName9.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                } 
			}
            cbbSupplName9.Text = Convert.ToString(cbbSupplName9.Items[0]);
			cbbSupplName9.Enabled = true;
			if (COcheck(cbbCode9.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage9.Font = ft;
			}
			if (txtPercentage9.Text != null)
			{
				if (cbbCode9.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage9.Text);
				}
			}	
		}
		private void cbbCode10_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode10.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName10.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                } 
			}
            cbbSupplName10.Text = Convert.ToString(cbbSupplName10.Items[0]);
			cbbSupplName10.Enabled = true;
			if (COcheck(cbbCode10.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage10.Font = ft;
			}
			if (txtPercentage10.Text != null)
			{
				if (cbbCode10.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage10.Text);
				}
			}	
		}
		private void cbbCode11_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode11.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName11.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
			}
            cbbSupplName11.Text = Convert.ToString(cbbSupplName11.Items[0]);
			cbbSupplName11.Enabled = true;
			if (COcheck(cbbCode11.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage11.Font = ft;
			}
			if (txtPercentage11.Text != null)
			{
				if (cbbCode11.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage11.Text);
				}
			}	
		}
		private void cbbCode12_SelectedIndexChanged(object sender, EventArgs e)
		{
			bool o = false;
			string selected = cbbCode12.Text;
			string[] SupplierName = _DBAccessory.getSupplierNameBycode(selected);
			for (int a = 0; a < SupplierName.Length; a++)
			{
                if (SupplierName[a] != null)
                {
                    cbbSupplName12.Items.Add(SupplierName[a]);
                }
                else
                {
                    break;
                }
			}
            cbbSupplName12.Text = Convert.ToString(cbbSupplName12.Items[0]);
			cbbSupplName12.Enabled = true;
			if (COcheck(cbbCode11.SelectedItem.ToString(), o))
			{
				Font ft = new Font("", 12, FontStyle.Bold);
				txtPercentage12.Font = ft;
			}
			if (txtPercentage12.Text != null)
			{
				if (cbbCode12.Text.Contains("Water"))
				{
					water += Convert.ToSingle(txtPercentage12.Text);
				}
			}	
		}
		#endregion
		private void cbbSupplName1_SelectedIndexChanged(object sender, EventArgs e)
		{
			string selectedsupplier = cbbSupplName1.Text;
			string selectedcode = cbbCode1.Text;
			Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
			txtTradeName1.Text = Supplier_RM.TradeName;
			txtKeyProperty1.Text = Supplier_RM.KeyProperty;
			txtCost1.Text = Convert.ToString(Supplier_RM.TotalCost);
		}
		private void txtPercentage1_TextChanged(object sender, EventArgs e)
		{
            if (txtPercentage1.Text != "" && txtCost1.Text != "")
            {
                float cost = Convert.ToSingle(txtCost1.Text);
                float percentage = Convert.ToSingle(txtPercentage1.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage1.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit1.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage2.Enabled = true;
                }
            }
            

		}
		private void txtCostUnit1_Validating(object sender, CancelEventArgs e)
		{
		    Regex digit=new Regex("^[0-9]+$");
			if (digit.IsMatch(txtCostUnit1.Text)) 
			{
				cbbCode2.Enabled = true;
			}
		}
		private void cbbSupplName2_SelectedIndexChanged(object sender, EventArgs e)
		{
            string selectedsupplier = cbbSupplName2.Text;
            string selectedcode = cbbCode2.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName2.Text = Supplier_RM.TradeName;
            txtKeyProperty2.Text = Supplier_RM.KeyProperty;
            txtCost2.Text = Convert.ToString(Supplier_RM.TotalCost);
		}
		private bool COcheck(string t,bool o) 
		{
			string[] x=t.Split(new char[]{'-'});
			foreach (string a in x)
			{
				if (a.Contains("CO"))
				{
					return true;
				}
			}
			return false;
		}
		private void lblPackage_TextChanged(object sender, EventArgs e)
		{
			package= Convert.ToSingle(lblPackage.Text);	
		}
		private void lblRawMat_TextChanged(object sender, EventArgs e)
		{
			mat = Convert.ToSingle(lblRawMat.Text);
		}
		private void btnCaculate_Click(object sender, EventArgs e)
		{
            if (txtRRP.Text != "" && lblTotal.Text != "" && lblObject.Text != "")
            {
                float RRP = Convert.ToSingle(txtRRP.Text);
                lblTotal.Text = Convert.ToString(package + mat);
                lblObject.Text = Convert.ToString(RRP * objective - package);
                if (txtCostUnit1.Text != null)
                {
                    mat += Convert.ToSingle(txtCost1.Text);
                }
                else if (txtCostUnit2.Text != null)
                {
                    mat += Convert.ToSingle(txtCost2.Text);
                }
                else if (txtCostUnit3.Text != null)
                {
                    mat += Convert.ToSingle(txtCost3.Text);
                }
                else if (txtCostUnit4.Text != null)
                {
                    mat += Convert.ToSingle(txtCost4.Text);
                }
                else if (txtCostUnit5.Text != null)
                {
                    mat += Convert.ToSingle(txtCost5.Text);
                }
                else if (txtCostUnit6.Text != null)
                {
                    mat += Convert.ToSingle(txtCost6.Text);
                }
                else if (txtCostUnit7.Text != null)
                {
                    mat += Convert.ToSingle(txtCost7.Text);
                }
                else if (txtCostUnit8.Text != null)
                {
                    mat += Convert.ToSingle(txtCost8.Text);
                }
                else if (txtCostUnit9.Text != null)
                {
                    mat += Convert.ToSingle(txtCost9.Text);
                }
                else if (txtCostUnit10.Text != null)
                {
                    mat += Convert.ToSingle(txtCost10.Text);
                }
                else if (txtCostUnit11.Text != null)
                {
                    mat += Convert.ToSingle(txtCost11.Text);
                }
                else if (txtCostUnit12.Text != null)
                {
                    mat += Convert.ToSingle(txtCost12.Text);
                }
                lblRawMat.Text = Convert.ToString(mat);
            }
		}

        private void cbbSupplName3_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName3.Text;
            string selectedcode = cbbCode3.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName3.Text = Supplier_RM.TradeName;
            txtKeyProperty3.Text = Supplier_RM.KeyProperty;
            txtCost3.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName4_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName4.Text;
            string selectedcode = cbbCode4.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName4.Text = Supplier_RM.TradeName;
            txtKeyProperty4.Text = Supplier_RM.KeyProperty;
            txtCost4.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName5_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName5.Text;
            string selectedcode = cbbCode5.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName5.Text = Supplier_RM.TradeName;
            txtKeyProperty5.Text = Supplier_RM.KeyProperty;
            txtCost5.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName6_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName6.Text;
            string selectedcode = cbbCode6.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName6.Text = Supplier_RM.TradeName;
            txtKeyProperty6.Text = Supplier_RM.KeyProperty;
            txtCost6.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName7_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName7.Text;
            string selectedcode = cbbCode7.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName7.Text = Supplier_RM.TradeName;
            txtKeyProperty7.Text = Supplier_RM.KeyProperty;
            txtCost7.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName8_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName8.Text;
            string selectedcode = cbbCode8.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName8.Text = Supplier_RM.TradeName;
            txtKeyProperty8.Text = Supplier_RM.KeyProperty;
            txtCost8.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName9_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName9.Text;
            string selectedcode = cbbCode9.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName9.Text = Supplier_RM.TradeName;
            txtKeyProperty9.Text = Supplier_RM.KeyProperty;
            txtCost9.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName10_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName10.Text;
            string selectedcode = cbbCode10.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName10.Text = Supplier_RM.TradeName;
            txtKeyProperty10.Text = Supplier_RM.KeyProperty;
            txtCost10.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName11_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName11.Text;
            string selectedcode = cbbCode11.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName11.Text = Supplier_RM.TradeName;
            txtKeyProperty11.Text = Supplier_RM.KeyProperty;
            txtCost11.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void cbbSupplName12_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedsupplier = cbbSupplName12.Text;
            string selectedcode = cbbCode12.Text;
            Supplier_RM Supplier_RM = _DBAccessory.getSupplierRMbyCodeAndSupplier(selectedcode, selectedsupplier);
            txtTradeName12.Text = Supplier_RM.TradeName;
            txtKeyProperty12.Text = Supplier_RM.KeyProperty;
            txtCost12.Text = Convert.ToString(Supplier_RM.TotalCost);
        }

        private void txtPercentage2_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage2.Text != "" && txtCost2.Text != "")
            {
                float cost = Convert.ToSingle(txtCost1.Text);
                float percentage = Convert.ToSingle(txtPercentage2.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage1.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit2.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage3.Enabled = true;
                }
            }
            
        }

        private void txtPercentage3_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage3.Text != "" && txtCost3.Text != "")
            {
                float cost = Convert.ToSingle(txtCost3.Text);
                float percentage = Convert.ToSingle(txtPercentage3.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage3.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit3.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage4.Enabled = true;
                }
            }
        }

        private void txtPercentage4_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage4.Text != "" && txtCost4.Text != "")
            {
                float cost = Convert.ToSingle(txtCost4.Text);
                float percentage = Convert.ToSingle(txtPercentage4.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage4.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit4.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage5.Enabled = true;
                }
            }

        }

        private void txtPercentage5_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage5.Text != "" && txtCost5.Text != "")
            {
                float cost = Convert.ToSingle(txtCost5.Text);
                float percentage = Convert.ToSingle(txtPercentage5.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage5.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit5.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage6.Enabled = true;
                }
            }
        }

        private void txtPercentage6_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage6.Text != "" && txtCost6.Text != "")
            {
                float cost = Convert.ToSingle(txtCost6.Text);
                float percentage = Convert.ToSingle(txtPercentage6.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage6.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit6.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage7.Enabled = true;
                }
            }
        }

        private void txtPercentage7_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage7.Text != "" && txtCost7.Text != "")
            {
                float cost = Convert.ToSingle(txtCost7.Text);
                float percentage = Convert.ToSingle(txtPercentage7.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage7.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit7.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage8.Enabled = true;
                }
            }
        }

        private void txtPercentage8_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage8.Text != "" && txtCost8.Text != "")
            {
                float cost = Convert.ToSingle(txtCost8.Text);
                float percentage = Convert.ToSingle(txtPercentage8.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage8.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit8.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage9.Enabled = true;
                }
            }
        }

        private void txtPercentage9_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage9.Text != "" && txtCost9.Text != "")
            {
                float cost = Convert.ToSingle(txtCost9.Text);
                float percentage = Convert.ToSingle(txtPercentage9.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage9.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit9.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage10.Enabled = true;
                }
            }
        }

        private void txtPercentage10_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage10.Text != "" && txtCost10.Text != "")
            {
                float cost = Convert.ToSingle(txtCost10.Text);
                float percentage = Convert.ToSingle(txtPercentage10.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage10.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit10.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage11.Enabled = true;
                }
            }
        }

        private void txtPercentage11_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage11.Text != "" && txtCost11.Text != "")
            {
                float cost = Convert.ToSingle(txtCost11.Text);
                float percentage = Convert.ToSingle(txtPercentage11.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage11.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit11.Text = Convert.ToString((cost * percentage / 100) * jar);
                    txtPercentage12.Enabled = true;
                }
            }
        }

        private void txtPercentage12_TextChanged(object sender, EventArgs e)
        {
            if (txtPercentage12.Text != "" && txtCost12.Text != "")
            {
                float cost = Convert.ToSingle(txtCost12.Text);
                float percentage = Convert.ToSingle(txtPercentage12.Text);
                float jar = Convert.ToSingle(cbbJar.SelectedItem.ToString());
                Regex digit = new Regex("(^[0-9]{1,})");
                if (!digit.IsMatch(txtPercentage12.Text))
                {
                    MessageBox.Show("Please Input the digit");
                }
                else
                {
                    txtCostUnit12.Text = Convert.ToString((cost * percentage / 100) * jar);
            
                }
            }
            
        }








		

	}
}
